@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改职级</title>
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <style>
        .layui-form {
            padding: 20px;
        }
        .layui-form-item .layui-input-inline {
            width: 220px;
        }
        .layui-form-label {
            width: 100px;
        }
        .layui-input-block {
            margin-left: 130px;
        }
        .required:after {
            content: '*';
            color: red;
            margin-left: 4px;
        }
        /* 自定义开关样式 */
        .switch-container {
            display: inline-flex;
            align-items: center;
            margin-left: 10px;
        }
        .auto-switch {
            position: relative;
            display: inline-flex;
            align-items: center;
            cursor: pointer;
            padding: 2px 8px;
            border-radius: 15px;
            background-color: #fff;
            border: 1px solid #ddd;
            transition: all 0.3s;
        }
        .auto-switch.checked {
            background-color: #26C6DA;
            border-color: #26C6DA;
        }
        .switch-text {
            color: #666;
            font-size: 14px;
            margin-right: 5px;
        }
        .auto-switch.checked .switch-text {
            color: #fff;
        }
        .switch-button {
            width: 18px;
            height: 18px;
            background-color: #fff;
            border-radius: 50%;
            transition: all 0.3s;
            box-shadow: 0 2px 4px rgba(0,0,0,0.2);
        }
        .auto-switch.checked .switch-button {
            margin-left: 5px;
        }
    </style>
</head>
<body>
    @Html.AntiForgeryToken()
    <form class="layui-form" lay-filter="editForm">
        <input type="hidden" name="id" />
        <div class="layui-form-item">
            <label class="layui-form-label">职级编码</label>
            <div class="layui-input-inline" style="width: 220px;">
                <input type="text" name="jobGradeCoding" class="layui-input" id="jobGradeCodingInput">
            </div>
            <div class="layui-input-inline" style="width: 120px;">
                <div class="auto-switch" id="autoGenerateSwitch">
                    <span class="switch-text">自动生成</span>
                    <div class="switch-button"></div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">职级层次</label>
            <div class="layui-input-block">
                <input type="text" name="jobGradeLevel" required lay-verify="required" placeholder="请输入职级层次" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">职级层次名称</label>
            <div class="layui-input-block">
                <input type="text" name="jobGradeLevelName" required lay-verify="required" placeholder="请输入职级层次名称" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">薪资范围</label>
            <div class="layui-input-inline" style="width: 150px;">
                <input type="number" name="salaryRangelower" required lay-verify="required|number" placeholder="下限" class="layui-input">
            </div>
            <div class="layui-form-mid">-</div>
            <div class="layui-input-inline" style="width: 150px;">
                <input type="number" name="salaryRangelimits" required lay-verify="required|number" placeholder="上限" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label required">备注</label>
            <div class="layui-input-block">
                <textarea name="referenceColumn" required lay-verify="required" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="editSubmit">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>

    <script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
        // 自动生成编码函数
        function generateJobGradeCode() {
            var now = new Date();
            var timestamp = now.getFullYear().toString() +
                          (now.getMonth() + 1).toString().padStart(2, '0') +
                          now.getDate().toString().padStart(2, '0') +
                          now.getHours().toString().padStart(2, '0') +
                          now.getMinutes().toString().padStart(2, '0') +
                          now.getSeconds().toString().padStart(2, '0');
            return 'JG' + timestamp;  // 改用JG前缀
        }

        layui.use(['form', 'layer'], function(){
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            var isAutoGenerate = false;

            // 开关点击事件
            $('#autoGenerateSwitch').on('click', function() {
                isAutoGenerate = !isAutoGenerate;
                $(this).toggleClass('checked');
                
                if (isAutoGenerate) {
                    var code = generateJobGradeCode();
                    $('#jobGradeCodingInput').val(code).prop('readonly', true);
                } else {
                    $('#jobGradeCodingInput').prop('readonly', false);
                }
            });

            // 获取URL中的ID参数
            var id = location.search.match(/id=(\d+)/)?.[1];
            if (!id) {
                layer.msg('参数错误', {icon: 2});
                return;
            }

            // 加载职级数据
            $.ajax({
                url: '/JobGradeManagement/GetJobGradeById',
                type: 'get',
                data: { id: id },
                success: function(res) {
                    // 填充表单
                    form.val('editForm', {
                        'id': res.id,
                        'jobGradeCoding': res.jobGradeCoding,
                        'jobGradeLevel': res.jobGradeLevel,
                        'jobGradeLevelName': res.jobGradeLevelName,
                        'salaryRangelower': res.salaryRangelower,
                        'salaryRangelimits': res.salaryRangelimits,
                        'referenceColumn': res.referenceColumn
                    });

                    // 设置开关状态
                    isAutoGenerate = !res.jobGradeCoding || res.jobGradeCoding.startsWith('JG');
                    if (isAutoGenerate) {
                        $('#autoGenerateSwitch').addClass('checked');
                        $('#jobGradeCodingInput').prop('readonly', true);
                    }
                },
                error: function(xhr) {
                    var msg = xhr.responseJSON?.message || '获取数据失败';
                    layer.msg(msg, {icon: 2});
                }
            });

            // 表单提交
            form.on('submit(editSubmit)', function(data) {
                // 验证薪资范围
                if (parseFloat(data.field.salaryRangelower) >= parseFloat(data.field.salaryRangelimits)) {
                    layer.msg('薪资范围上限必须大于下限', {icon: 2});
                    return false;
                }

                var loadingIndex = layer.load(1, {
                    shade: [0.3, '#fff']
                });

                $.ajax({
                    url: '/JobGradeManagement/UpdateJobGrade',
                    type: 'post',
                    contentType: 'application/json',
                    data: JSON.stringify(data.field),
                    headers: {
                        'RequestVerificationToken': $('input[name="__RequestVerificationToken"]').val(),
                        'X-Requested-With': 'XMLHttpRequest'
                    },
                    success: function(res) {
                        layer.close(loadingIndex);
                        if (res.success) {
                            layer.msg(res.message || '修改成功', {
                                icon: 1,
                                time: 1000
                            }, function() {
                                // 刷新父页面
                                parent.location.reload();
                                // 关闭当前弹窗
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.message || '修改失败', {icon: 2});
                        }
                    },
                    error: function(xhr) {
                        layer.close(loadingIndex);
                        var msg = xhr.responseJSON?.message || '修改失败';
                        layer.msg(msg, {icon: 2});
                    }
                });

                return false;
            });
        });
    </script>
</body>
</html> 